<?php
session_start();
function randomText($count, $rm_similar = false)
{
    // create list of characters
    $chars = array_flip(array_merge(range(0, 9), range('A', 'Z')));

    // remove similar looking characters that might cause confusion
    if ($rm_similar)
    {
        unset($chars[0], $chars[1], $chars[2], $chars[5], $chars[8],
            $chars['B'], $chars['I'], $chars['O'], $chars['Q'],
            $chars['S'], $chars['U'], $chars['V'], $chars['Z']);
    }

    // generate the string of random text
    for ($i = 0, $text = ''; $i < $count; $i++)
    {
        $text .= array_rand($chars);
    }

    return $text;
}
 $code=randomText(4); //随机生成的字符串
 $width = 50; //验证码图片的宽度
 $height = 20; //验证码图片的高度
 @header("Content-Type:image/png");
 $_SESSION["login_check_number"] = strtoupper($code);
 $im=imagecreate($width,$height);
 //背景色
 $back=imagecolorallocate($im,255, 255, 255);
 imagefill($im, 0, 0, $back);
 //模糊点颜色
 $pix=imagecolorallocate($im,187,230,247);
 //字体色
 $font=imagecolorallocate($im,41,163,238);
 //绘模糊作用的点
 for($i=0;$i <1000;$i++)
 {
  imagesetpixel($im,rand(0,$width),rand(0,$height),$pix);
 }
 imagestring($im, 5, 7, 2,$code, $font);
 imagerectangle($im,0,0,$width-1,$height-1,$font);
 imagepng($im);
 imagedestroy($im);
?>